package com.company.project.common.utils;

import com.alibaba.fastjson.JSONObject;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.*;
import com.dingtalk.api.response.*;

public class DingToolsUtils {

    public static String AppKey = "dingo6zoxatpqnlllgfa";
    public static String AppSecret = "kYQCnseqJbxYUKwDLdTt67IMSjAAuVgCt2gsDJrRKDcbo7meeAIAbNs6cuMStyh_";

    public static String CorpId = "ding423e67b11c22f388";
    public static String AgentId = "487999217";

    public static String encodingAesKey = "7777xxxxlvdhntotr3x9qhlbytb18zyz5zxxxxx4396";
    public static String token = "06fe4c68f15437649b37ef8a07a53876";


    public static void sendMsg(String accessToken) throws Exception{
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
        OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
        request.setAgentId(Long.valueOf(AgentId));
        request.setUseridList("17026334127878107");
        request.setToAllUser(false);

        OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
        msg.setMsgtype("text");
        msg.setText(new OapiMessageCorpconversationAsyncsendV2Request.Text());
        msg.getText().setContent("test123");
        request.setMsg(msg);

        msg.setMsgtype("image");
        msg.setImage(new OapiMessageCorpconversationAsyncsendV2Request.Image());
        msg.getImage().setMediaId("@lADOdvRYes0CbM0CbA");
        request.setMsg(msg);

        msg.setMsgtype("file");
        msg.setFile(new OapiMessageCorpconversationAsyncsendV2Request.File());
        msg.getFile().setMediaId("@lADOdvRYes0CbM0CbA");
        request.setMsg(msg);

        msg.setMsgtype("link");
        msg.setLink(new OapiMessageCorpconversationAsyncsendV2Request.Link());
        msg.getLink().setTitle("test");
        msg.getLink().setText("test");
        msg.getLink().setMessageUrl("test");
        msg.getLink().setPicUrl("test");
        request.setMsg(msg);

        msg.setMsgtype("markdown");
        msg.setMarkdown(new OapiMessageCorpconversationAsyncsendV2Request.Markdown());
        msg.getMarkdown().setText("#### 价格预警   \n   <font color=#e60020>售价低于供货价</font>   \n ");
        msg.getMarkdown().setTitle("价格预警");
        request.setMsg(msg);

        /*msg.setOa(new OapiMessageCorpconversationAsyncsendV2Request.OA());
        msg.getOa().setHead(new OapiMessageCorpconversationAsyncsendV2Request.Head());
        msg.getOa().getHead().setText("head");
        msg.getOa().setBody(new OapiMessageCorpconversationAsyncsendV2Request.Body());
        msg.getOa().getBody().setContent("xxx");
        msg.setMsgtype("oa");
        request.setMsg(msg);

        msg.setActionCard(new OapiMessageCorpconversationAsyncsendV2Request.ActionCard());
        msg.getActionCard().setTitle("xxx123411111");
        msg.getActionCard().setMarkdown("### 测试123111");
        msg.getActionCard().setSingleTitle("测试测试");
        msg.getActionCard().setSingleUrl("https://www.dingtalk.com");
        msg.setMsgtype("action_card");
        request.setMsg(msg);*/
        OapiMessageCorpconversationAsyncsendV2Response rsp = client.execute(request, accessToken);
        System.out.println(rsp.getBody());
    }

    public static void main(String[] args) {
            try {
                DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
                OapiGettokenRequest request = new OapiGettokenRequest();
                request.setAppkey(AppKey);
                request.setAppsecret(AppSecret);
                request.setHttpMethod("GET");
                OapiGettokenResponse response = client.execute(request);
                String  result = response.getBody();
                System.out.println(result);
                JSONObject jsonObject = JSONObject.parseObject(result);
                String accessToken = jsonObject.getString("access_token");
                //{"name":"高岗","userid":"17026334127878107"}
                sendMsg(accessToken);
                //获取部门列表
                /*DingTalkClient clientDep = new DefaultDingTalkClient("https://oapi.dingtalk.com/department/list");
                OapiDepartmentListRequest reqDep = new OapiDepartmentListRequest();
                reqDep.setLang("zh_CN");
                reqDep.setFetchChild(true);
                reqDep.setId("911858183");
                reqDep.setHttpMethod("GET");
                OapiDepartmentListResponse rspDep = clientDep.execute(reqDep, accessToken);
                System.out.println(rspDep.getBody());
                JSONObject jsonObjectDept = JSONObject.parseObject(rspDep.getBody());*/

                //获取部门下人员列表
                /*DingTalkClient client2 = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/simplelist");
                OapiUserSimplelistRequest req = new OapiUserSimplelistRequest();
                req.setLang("zh_CN");
                req.setDepartmentId(663808122L);
                req.setOffset(0L);
                req.setSize(20L);
                req.setOrder("entry_asc");
                req.setHttpMethod("GET");
                OapiUserSimplelistResponse rsp = client2.execute(req, accessToken);
                System.out.println(rsp.getBody());*/


            }catch (Exception e){
                e.printStackTrace();
            }

    }
}
